﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.WebPages.Html;
using MiscHelper;

namespace App_Code
{
    /// <summary>
    /// Summary description for HotelController
    /// </summary>
    public class HotelController : IFlushDBCache
    {
        private readonly Dictionary<int, HotelRecord> _records = new Dictionary<int, HotelRecord>();

        readonly Dictionary<string, string> _cities = new Dictionary<string, string>();
        readonly object _lock = new object();

        private static string EmailHelper(string email)
        {
            // <a href="mailo:no-one@snai1mai1.com">example</A>
            return "<a href=\"mailto:" + email + "\">" + email + "</a>";
        }

        private static string WebHelper(string webUrl)
        {
            return "<a href=\"http://" + webUrl + "\">" + webUrl + "</a>";
        }

        public string GetAllRecordsForLocationAsHTML(string location)
        {
            if (_records.Count() == 0)
            {
                return "";
            }
            if (location == "")
            {
                location = _records.First().Value.CityShortCode;
            }

            var sb = new StringBuilder();

            var r = 0;

            foreach (var record in _records)
            {
                if (record.Value.CityShortCode == location)
                {
                    sb.Append((r & 1) == 0 ? "<div class=\"ghrowdark\">" : "<div class=\"ghrowlight\">");
                    r++;
                    AddChunk(record.Value, sb);
                    sb.Append("</div>");
                }
            }
            return sb.ToString();
        }

        private void AddChunk(HotelRecord record, StringBuilder sb)
        {
            sb.Append("<div style='margin: 10px 10px 10px 10px;'>");
            sb.Append("<p>");
            sb.Append("<b>");
            sb.Append(record.Name);
            sb.Append("</b>");
            sb.Append("<br />");
            sb.Append(record.AddressLine1);
            sb.Append("<br />");
            sb.Append(record.AddressLine2);
            sb.Append("<br />");

            {
                string phone = PhoneNumber.Sanitize(record.Telephone);
                if (phone != "")
                {
                    AppendFieldName("Tel", sb);
                    sb.Append(phone);
                    sb.Append("<br />");
                }
            }

            {
                string fax = PhoneNumber.Sanitize(record.Fax);
                if (fax != "")
                {
                    AppendFieldName("Fax", sb);
                    sb.Append(fax);
                    sb.Append("<br />");
                }
            }

            var emails = EmailFieldParser.Parse(record.Email);
            foreach (var emailAddress in emails)
            {
                AppendFieldName("Email", sb);
                sb.Append(EmailHelper(emailAddress));
                sb.Append("<br />");
            }

            var webAddresses = WebAddressParser.Parse(record.WebURL);

            foreach (var w in webAddresses)
            {
                AppendFieldName("Web", sb);
                sb.Append(WebHelper(w));
                sb.Append("<br />");
            }
            sb.Append("</p>");
            sb.Append("</div>");
    
        }
        // <p style="display: inline-block; width: 100px;">Select City: </p>

        void AppendFieldName(string field, StringBuilder sb)
        {
            sb.Append("<span style='display: inline-block; width: 60px;'>");
            sb.Append(field);
            sb.Append(": </span>");
        }

        public HotelController()
        {
            ReReadCache();
        }

        public IEnumerable<SelectListItem> GetCities()
        {
            var l = new List<SelectListItem> { new SelectListItem { Text = "Select...", Value = "" } };
            l.AddRange(_cities.Select(kv => new SelectListItem { Text = kv.Value, Value = kv.Key }));
            return l;
        }

        public Dictionary<string, object> GetAttributes()
        {
            return new Dictionary<string, object>
                           {
                               {"onChange", "getHotels();"},
                               {"onkeyup", "getHotels();"},
                               {"style", "height:26px;width:220px;margin-left: 0px"}
                           };
        }

        private void ReReadCache()
        {
            lock (_lock)
            {
                _records.Clear();
                _cities.Clear();
                HotelDB.GetHotels(_records);
                HotelDB.GetCityLookUp(_cities);
            }
        }

        public void FlushDBCache()
        {
            ReReadCache();
        }
    }

    interface IFlushDBCache
    {
        void FlushDBCache();
    }
    
}